﻿using System.Text;

namespace FluentTagsGenerator
{
    class TextMethodGenerator
    {
        const string methodAddTextTemplate = @"
        public ${Class}Tag Text(string text)
        {
            Controls.Add(new LiteralControl(text));
            return this;
        }

        public ${Class}Tag Text(object value)
        {
            Controls.Add(new LiteralControl(SafeString(value)));
            return this;
        }
        ";

        const string methodGenericAddTextTemplate = @"
        public ${Class}Tag<ParentType> Text(string text)
        {
            Controls.Add(new LiteralControl(text));
            return this;
        }
        
        public ${Class}Tag<ParentType> Text(object value)
        {
            Controls.Add(new LiteralControl(SafeString(value)));
            return this;
        }
        ";

        public string ClassName { get; private set; }
        public StringBuilder ClassContent { get; private set; }
        public bool IsText { get; private set; }

        public TextMethodGenerator(string className, StringBuilder classContent, bool isText)
        {
            ClassName = className;
            ClassContent = classContent;
            IsText = isText;
        }

        public StringBuilder Generate()
        {
            ClassContent = GenerateTextMethod(methodAddTextTemplate, "${textMethod}");
            ClassContent = GenerateTextMethod(methodGenericAddTextTemplate, "${genericTextMethod}");
            
            return ClassContent;
        }

        private StringBuilder GenerateTextMethod(string template, string ToReplace)
        {
            string method = "";
            
            if (IsText)
            {
                method =
                    template.Replace("${Class}", ClassName);
            }

            ClassContent = ClassContent.Replace(ToReplace, method);

            return ClassContent;
        }
        
    }
}
